#pragma once
#include "Client.h"
#include "CallerMachine.h"
#include <fstream>
#include <cstdio>
#include <qmessagebox.h>
int read(std::string url,LinkList<Client>& list,int cur) {
    ifstream fp;
    char line[1024] = { '\0' };
    // 打开文件
    fp.open(url, ios::in);
    if (!fp.is_open()) {
        QMessageBox openFail(QMessageBox::Question, "error", "文件未能打开\n请检查路径");
        openFail.exec();
        return 0;
    }
    CallerMachine caller;                   //叫号器
    char name[10] = {'\0'};                 //姓名
    char sexChar[4] = { '\0' };             //性别
    char bussinessTypeStr[15] = { '\0' };   //业务类型(字符串)
    BussinessType bussinessType;            //业务类型
    int reachTime, comsumptionTime;         //到达时间 消耗时间
    int clientCount = cur;                  //客户索引
    while (fp.getline(line, 1024,'\n')) {
         //解析每一行 
         int n = sscanf_s(line, "%s %s %s %d %d", name,10, sexChar, 4, bussinessTypeStr,15, &reachTime, &comsumptionTime);
        // 将字符转换为枚举类型
        Sex clientSex = (strcmp(sexChar, "男") == 0) ? Sex::MAN : Sex::WOMAN;
        bussinessType = (strcmp(bussinessTypeStr, "特殊业务") == 0) ? BussinessType::SPECIAL : BussinessType::COMMON;
        // 存储到链表中
        bool ok = list.setElement(*new Client(caller, name, clientSex, bussinessType, reachTime, comsumptionTime),clientCount);
        if (!ok) {
            list.insert(*new Client(caller, name, clientSex, bussinessType, reachTime, comsumptionTime), clientCount);
        }
        clientCount++;
    }
    // 关闭文件
    fp.close();
    // 返回客户数
    return clientCount;
}